﻿using Role_el.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Role_el.Controllers
{
    public class ModulesController : ApiController
    {
        /// <summary>
        /// 获取模块列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetMoList(int page,int limit)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var AuthorizeInfo = db.Modules.Where(p => true);
                var molist = from a in AuthorizeInfo
                               where a.ParentId == 0
                               select new
                               {
                                   Id = a.Id,
                                   Name = a.Name,
                                   ParentId = a.ParentId,
                                   Path = a.Path,
                                   Weight = a.Weight,
                                   children = from t in AuthorizeInfo
                                              where t.ParentId == a.Id
                                              select new
                                              {
                                                  Id = t.Id,
                                                  Name = t.Name,
                                                  ParentId = t.ParentId,
                                                  Path = t.Path,
                                                  Weight = t.Weight
                                              }
                               };
                var count = molist.Count();
                return Json(new {count=count, data = molist.OrderByDescending(p=>p.Id).Skip((page-1)*limit).Take(limit).ToList() });
            }
        }
        /// <summary>
        /// 获取父级分类
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IHttpActionResult GetTypeList()
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var molist = db.Modules.Where(p => p.ParentId == 0);
                var mo = molist.Select(p => new
                {
                    Id=p.Id,
                    Name=p.Name
                });
                return Json(new { data=mo.ToList() });
            }
        }
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="mo"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult AddMo([FromBody] Modules mo)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Modules r = db.Modules.FirstOrDefault(p => p.Name == mo.Name);
                if (r == null)
                {
                    db.Modules.Add(mo);

                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new
                        {
                            code = 0,
                            msg = "添加成功！"
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            code = -1,
                            msg = "添加失败！"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = -2,
                        msg = "该模块已存在！"
                    });
                }
            }
        }
        /// <summary>
        /// 编辑模块
        /// </summary>
        /// <param name="mo"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult EditMo([FromBody] Modules mo)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                Modules molist = db.Modules.SingleOrDefault(p => p.Id == mo.Id);
                molist.Name = mo.Name;
                molist.ParentId = mo.ParentId;
                molist.Path = mo.Path;
                molist.Weight = mo.Weight;
                Modules r = db.Modules.FirstOrDefault(p => p.Name == mo.Name);
                if (r == null)
                {
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new
                        {
                            code = 0,
                            msg = "修改成功！"
                        });
                    }
                    else
                    {
                        return Json(new
                        {
                            code = -1,
                            msg = "修改失败！"
                        });
                    }
                }
                else
                {
                    return Json(new
                    {
                        code = -2,
                        msg = "该模块已存在！"
                    });
                }

            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mo"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult DelMo([FromBody] Modules mo)
        {
            using (RolesDBEntities db = new RolesDBEntities())
            {
                var u = db.RoleModules.FirstOrDefault(p => p.ModuleId == mo.Id);
                if (u == null)
                {
                    db.Entry<Modules>(mo).State = EntityState.Deleted;
                    int n = db.SaveChanges();
                    if (n > 0)
                    {
                        return Json(new { code = 0, msg = "删除成功！" });
                    }
                    else
                    {
                        return Json(new { code = -1, msg = "删除失败！" });
                    }
                }
                else
                {
                    return Json(new { code = -2, msg = "该模块正在被使用，不能删除！" });
                }
            }
        }
    }
}
